package org.sith.gl.taskmanager.server.db.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sith.gl.taskmanager.server.db.dao.UserEntity;
import org.sith.gl.taskmanager.server.db.service.UserService;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.JpaCallback;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;


public class UserServiceImpl extends DefaultDAOServiceImpl<UserEntity> implements UserService {
     private static final Log log = LogFactory.getLog(UserServiceImpl.class);
    @Override
    public UserEntity findByLogin(final String username) {

        try {
            return (UserEntity) getJpaTemplate().execute(new JpaCallback<Object>() {
                 @Override
                 public Object doInJpa(EntityManager em) throws PersistenceException {

                     final Query query = em.createQuery("select u from UserEntity u where u.login=:login");
                     query.setParameter("login",username);
                     return query.getSingleResult();
                 }
             });
        } catch (DataAccessException e) {
            log.warn(e);
            return null;
        }

    }
}
